package com.utils;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.DisplayMetrics;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import java.io.File;
import java.util.List;
import java.util.Locale;

/**
 * @describe
 * @author: lixiaopeng
 * @Date: 2019-05-29
 */
public final class OsUtils {

    public static final int REQUEST_CODE_PICK_IMAGE = 10001;
    public static final int REQUEST_CODE_CROP = 10002;
    public static final int REQUEST_CODE_CAMERA = 10003;

    /**
     * 获取屏幕宽，高度包括通知栏和虚拟按键
     *
     * @return
     */
    public static int getWinWide(@NonNull Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int widthPixels = dm.widthPixels;
        return widthPixels;
    }


    /**
     * 获取屏幕高
     *
     * @return
     */
    public static int getWinHeight(@NonNull Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int heightPixels = dm.heightPixels;
        return heightPixels;
    }

    /**
     * 获取可编辑区域的高度
     *
     * @param activity
     * @return
     */
    public static int getDisHeight(@NonNull Activity activity) {
        /**
         * 获取状态栏高度——方法1
         * */
        int heightPixels = -1;
        //获取status_bar_height资源的ID
        Rect outRect1 = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
        heightPixels = outRect1.height();
        return heightPixels;
    }

    /**
     * 获取可编辑区域的高度
     *
     * @param activity
     * @return
     */
    public static int getDisWidth(@NonNull Activity activity) {
        /**
         * 获取状态栏高度——方法1
         * */
        int heightPixels = -1;
        //获取status_bar_height资源的ID
        Rect outRect1 = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
        heightPixels = outRect1.width();
        return heightPixels;
    }

    public static int getStatusBarHeight(@NonNull Context context) {
        /**
         * 获取状态栏高度——方法1
         * */
        int statusBarHeight1 = -1;
        //获取status_bar_height资源的ID
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = context.getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight1;
    }

    /**
     * 打开系统相机拍照
     *
     * @param activity
     * @param picPath
     */
    public static Uri startSystemCamera(@NonNull Activity activity, @NonNull String picPath) {
        return startSystemCamera(null, activity, picPath);
    }

    public static Uri startSystemCamera(Fragment fragment, Activity activity, String picPath) {
        Uri uri = null;
        if (activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File file = new File(picPath);
            //调用系统相机
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                cameraIntent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri = PathUtils.fileToUri(file));
            if (fragment != null) {
                fragment.startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA);
            } else {
                activity.startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA);
            }
        } else {
            ToastUtils.toastText(activity, "没有系统相机");
        }
        return uri;
    }

    /**
     * 裁剪图片
     *
     * @param activity
     * @param uri
     * @param outFilePath
     * @param wide
     * @param height
     */
    public static void startPictureCrop(@NonNull Activity activity, @NonNull Uri uri, @NonNull String outFilePath, int wide, int height) {
        startPictureCrop(null, activity, uri, outFilePath, wide, height);
    }

    public static void startPictureCrop(Fragment fragment, Activity activity, @NonNull Uri uri, @NonNull String outFilePath, int wide, int height) {
        try {
            Intent intent = new Intent("com.android.camera.action.CROP");
            File file = new File(outFilePath);
            intent.setDataAndType(uri, "image/*");
            intent.putExtra("crop", "true");//可裁剪
            if (OsUtils.isHuaweiRom()) {
                //解决华为裁切不是矩形的问题
                intent.putExtra("aspectX", 9999);
                intent.putExtra("aspectY", 9998);
            } else {
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
            }
            intent.putExtra("outputX", wide);
            intent.putExtra("outputY", height);
            intent.putExtra("scale", true);
            //注意这里的uri不要使用FileProvider.getUriForFile,这里的源文件是第三方app所得
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            intent.putExtra("return-data", false);//若为false则表示不返回数据
            intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
            intent.putExtra("noFaceDetection", true);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                //添加这一句表示对目标应用临时授权该Uri所代表的文件
                intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
            if (fragment != null) {
                fragment.startActivityForResult(intent, REQUEST_CODE_CROP);
            } else {
                activity.startActivityForResult(intent, REQUEST_CODE_CROP);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 打开应用商店
     *
     * @param context
     */
    public static void launchAppMarket(Context context) {
        Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.getApplicationContext().startActivity(intent);
    }

    public static boolean isHuaweiRom() {
        try {
            String brand = Build.BRAND.toLowerCase(Locale.ENGLISH);
            return brand.contains("huawei") || brand.contains("honor");
        } catch (Throwable th) {
            return false;
        }
    }

    public static boolean isXiaomiRom() {
        try {
            return Build.BRAND.toLowerCase(Locale.ENGLISH).equals("xiaomi") || Build.MANUFACTURER.toLowerCase(Locale.ENGLISH).equals("xiaomi");
        } catch (Throwable th) {
            return false;
        }
    }

    public static boolean isMeizuRom() {
        try {
            return Build.DISPLAY.toLowerCase(Locale.ENGLISH).contains("flyme");
        } catch (Throwable th) {
            return false;
        }
    }

    public static boolean isOppo() {
        try {
            return Build.BRAND.toUpperCase(Locale.ENGLISH).contains("OPPO");
        } catch (Throwable e) {
            return false;
        }
    }

    public static boolean isVivo() {
        try {
            String brand = Build.BRAND.toLowerCase(Locale.ENGLISH);
            return brand.contains("vivo") || brand.contains("bbk");
        } catch (Throwable e) {
            return false;
        }
    }

    public static void startImagePicker(Activity activity){
        Intent intentSkip = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intentSkip.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        activity.startActivityForResult(intentSkip, REQUEST_CODE_PICK_IMAGE);
    }
}
